/**  
 * @Title: HttpComponent.java
 * @Description: TODO(描述)
 * @author liuyi
 * @date 2023-07-04 11:10:37 
 */  
package com.fsxgt.datagrid.core.component;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import lombok.extern.slf4j.Slf4j;

/**  
 * @ClassName: HttpComponent
 * @Description: TODO(描述)
 * @author liuyi
 * @date 2023-07-04 11:10:37 
*/
@Component
@Slf4j
public class HttpComponent {

	@Autowired
    private RestTemplate restTemplate;
	@Autowired
    private RetryTemplate retryTemplate;
	
	public <T> T sendHttpPostRetry(String url, Map param, Class<T> responseType) {
		HttpHeaders headers = new HttpHeaders();
		headers.add("Content-Type", "application/json; charset=UTF-8");

		HttpEntity<HashMap<String, Object>> httpEntity = new HttpEntity(param, headers);
		
		T t = retryTemplate.execute(context -> {
		    //执行请求操作
			log.info("发送请求"+url);
		    return restTemplate.postForObject(url, httpEntity, responseType);
		});
		
		return t;
	}
}
